package com.zxl.yjb.app;

import android.app.Activity;
import android.content.Context;

import com.zxl.yjb.activity.MainActivity;
import com.zxl.yjb.im.broadcast.BroadcastManager;
import com.zxl.yjb.utils.LogUtil;
import com.zxl.yjb.utils.SaveBeanUtil;

import java.util.Stack;

import cn.jpush.android.api.JPushInterface;

/**
 * Activity的管理类
 * Created by Administrator on 2017/5/23.
 */

public class AppManager {

    private static Stack<Activity> activityStack;

    private static AppManager instance;

    private static final String TAG = "AppManager";

    /**
     * 单一实例
     *
     * @return
     */
    public static AppManager getInstance() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity(堆栈中的最后一个)
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 获取指定类名的Activity
     */
    public Activity findActivity(Class<?> cls) {
        Activity targetActivity = null;
        if (activityStack != null) {
            for (Activity activity : activityStack) {
                if (activity.getClass().equals(cls))
                    targetActivity = activity;
            }
        }
        return targetActivity;
    }

    /**
     * 结束当前的Activity(堆栈中的最后一个)
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null && activityStack.size() > 0 && activityStack.contains(activity)) {
            LogUtil.e(TAG,"finishActivity",activity.getClass().getSimpleName());
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束所有的Activity
     */
    public void finishAllActivity() {
        for (Activity activity : activityStack) {
            activity.finish();
        }
        activityStack.clear();
    }
    /**
     * 退出应用程序
     */
    @SuppressWarnings("deprecation")
    public void AppExit(Context context) {
        try {
            //清空缓存
            SaveBeanUtil.putBean(context, Const.VIPINFO, "");
            SaveBeanUtil.putBean(context, Const.FILLMYINFO, "");
            SaveBeanUtil.putBean(context, Const.LOCATION_SELECT_PROVINCE,"");
            SaveBeanUtil.putBean(context, Const.LOCATION_SELECT_CITY,"");
            SaveBeanUtil.putBean(context, Const.LOCATION_SELECT_DISTRICT,"");
            SaveBeanUtil.putBean(context, Const.LOCATION_SELECT_STREET,"");
            //清空推送
            JPushInterface.deleteAlias(context,Const.JGTS_SEQUENCE);
           //推出Im
            finishAllActivity();
            BroadcastManager.getInstance(context).sendBroadcast(Const.EXIT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
